/**********************************************************************************
 * Code license:  GNU General Public License v3                                   *
 * Main page:     https://code.google.com/p/library-cross-platform/               *
 * Issue tracker: https://code.google.com/p/library-cross-platform/issues/list    *
***********************************************************************************/

package library.util;

public class LoopBuffer implements LoopBufferI
{
  protected int     buffsize;
  protected Object  obj_data[] = null;
  protected int     buf_pos;
  protected int     get_pos;

  public LoopBuffer()
  {
    this(100);
  }
  
  public LoopBuffer(int size)
  {
    setBufferSize(size);
  }
  
  public void setBufferSize(int s)
  {
    buffsize = s;
    if(obj_data == null)
     obj_data = new Object[10];
  }
  
  public int getBufferSize()
  {
    return buffsize;
  }
  
  public int add(Object o)
  {
    if(buf_pos >= buffsize || obj_data[buf_pos] != null)
      return -1;
    else
    {
      obj_data[buf_pos ++] = o;
      buf_pos = buf_pos >= buffsize ? 0 : buf_pos;
    }
    return buf_pos;
  }
  
  public Object get()
  {
    Object ret_value;
    if(get_pos >= buffsize)
     get_pos = 0;
    
    ret_value = obj_data[get_pos];
    obj_data[get_pos] = null;
    get_pos ++;
    return ret_value;
  }
}
